package com.aide.simplification.popedom.taglib;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.ecs.html.Input;
import org.apache.ecs.html.Option;
import org.apache.ecs.html.Script;
import org.apache.ecs.html.Select;

import com.aide.simplification.global.Utils;
import com.aide.simplification.popedom.login.LoginUser;
import com.aide.simplification.popedom.login.PopSSO;
import com.aide.simplification.popedom.popedom.User;
import com.aide.simplification.popedom.popedom.base.PopedomOP;

public class UserUpComboxTag extends TagSupport {

	private static final long serialVersionUID = 7361489650326503564L;
	private String name = "";
	private String id = "";
	private String style = "";
	private boolean multiple = false;
	private String attributes = "";
	private String defaultValue = "";
	private String pop = "0";
	private int level = 99;
	private boolean isAll = false;

	public int doEndTag() throws JspException {
		ServletRequest request = pageContext.getRequest();
		ServletResponse response = pageContext.getResponse();
		JspWriter out = pageContext.getOut();
//		ApplicationContext ctx = WebApplicationContextUtils
//				.getWebApplicationContext(pageContext.getServletContext());
//		User user = (User) ctx.getBean("User");
		User user = new User();
		Input input = new Input();
		input.setType("hidden");
		input.setName(name);
		input.setID(name);
		input.setValue("");

		Select select = new Select();
		if (!"".equals(style)) {
			select.setStyle(style);
		}
		select.setName(id);
		select.setID(id);
		select.setMultiple(multiple);

		if (!attributes.equals("")) {
			Pattern pattern = Pattern.compile("\\s+");
			String[] attribute = pattern.split(attributes);
			for (int i = 0; i < attribute.length; i++) {
				String[] temp = attribute[i].split("=");
				if (temp != null && temp.length == 2) {
					select.addAttribute(temp[0], temp[1]);
				} else if (temp.length == 1) {
					select.addAttribute(temp[0], "");
				}
			}
		}
		String useruuid = "";
		PopSSO sso = new PopSSO(request, response);
		LoginUser lu = sso.ssoSync();
		if (lu != null) {
			useruuid = lu.getUseruuid();
			if(level==99){
				level=lu.getLevel();
			}
		}

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (isAll) {
			list = user.findAllUserState();
		} else {
			list = user.findUserByUserUP(useruuid);
		}
		Script script = new Script();
		script.setType("text/javascript");
		script.setTagText("var $j;if(!$j){$j = jQuery.noConflict()};");
		try {
			out.println(script.toString());
		} catch (IOException e) {
		}
		PopedomOP popop = new PopedomOP();
		Option option1 = new Option("请选择", "", "请选择");
		select.addElement(option1);
		for (Iterator<Map<String, Object>> iterator = list.iterator(); iterator
				.hasNext();) {
			Map<String, Object> map = iterator.next();
			if ((pop.equals("0") || popop.checkPopAnd(pop, (String) map
					.get("pop")))
					&& level <= (Integer) map.get("userlevel")
					&& !map.get("useruuid").toString().equals(Utils.getConfig("pop.admin"))) {

				Option option = new Option(map.get("name").toString(), map.get(
						"useruuid").toString(), map.get("name").toString());
				if (map.get("useruuid").toString().equals(this.defaultValue)) {
					option.setSelected(true);
					input.setValue(map.get("name").toString());
				}
				select.addElement(option);
			}
		}
		select.setOnChange("$j('#" + name + "').val($j('#" + id
				+ " option:selected').text());");
		try {
			out.println(input.toString() + select.toString());
		} catch (IOException e) {
		}
		return EVAL_PAGE;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getAttributes() {
		return attributes;
	}

	public void setAttributes(String attributes) {
		this.attributes = attributes.trim();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public boolean isAll() {
		return isAll;
	}

	public void setAll(boolean isAll) {
		this.isAll = isAll;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getPop() {
		return pop;
	}

	public void setPop(String pop) {
		this.pop = pop;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isMultiple() {
		return multiple;
	}

	public void setMultiple(boolean multiple) {
		this.multiple = multiple;
	}
}
